package cool.qingqing.error;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cool.qingqing.result.Result;
import cool.qingqing.result.ResultEnum;

@RestController
public class HttpErrorController implements ErrorController {

    private final static String ERROR_PATH = "/error";


    @RequestMapping(value = ERROR_PATH)
    public Result errorJson(HttpServletRequest request) {
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        
        if(statusCode==ResultEnum.Bad_Request.getCode()){
            return new Result<>(ResultEnum.Bad_Request);
        }else if(statusCode==ResultEnum.Unauthorized.getCode()){
             return new Result<>(ResultEnum.Unauthorized);
        }else if(statusCode==ResultEnum.Forbidden.getCode()){
            return new Result<>(ResultEnum.Forbidden);
        }else if(statusCode==ResultEnum.Not_Found.getCode()){
            return new Result<>(ResultEnum.Not_Found);
        }else if(statusCode==ResultEnum.Method_Not_Allowed.getCode()){
            return new Result<>(ResultEnum.Method_Not_Allowed);
        }else if(statusCode==ResultEnum.Gone.getCode()){
            return new Result<>(ResultEnum.Gone);
        }else if(statusCode==ResultEnum.Unsupported_Media_Type.getCode()){
            return new Result<>(ResultEnum.Unsupported_Media_Type);
        }else if(statusCode==ResultEnum.Unprocessable_Entity.getCode()){
            return new Result<>(ResultEnum.Unprocessable_Entity);
        }else if(statusCode==ResultEnum.Too_Many_Requests.getCode()){
            return new Result<>(ResultEnum.Too_Many_Requests);
        }else if(statusCode==ResultEnum.Internal_Server_Error.getCode()){
            return new Result<>(ResultEnum.Internal_Server_Error);
        }else if(statusCode==ResultEnum.Service_Unavailable.getCode()){
            return new Result<>(ResultEnum.Service_Unavailable);
        }else{
            return new Result<>(ResultEnum.FAIL,null,"服务器维护中。");
        }
    


    }

    @Override
    public String getErrorPath() {
        return ERROR_PATH;
    }
}
